package com.aiit.shiro.realms;

import com.aiit.pojo.Perms;
import com.aiit.pojo.Role;
import com.aiit.pojo.User;
import com.aiit.service.UserService;
import com.aiit.utils.ApplicationContextUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LuTao
 * @Date: 2022/03/02/14:30
 */
/*自定义realm*/
public class CustomerRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取身份信息
       String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
      //根据主身份信息获取角色和权限信息
        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
        User user= userService.findRolesByUserName(primaryPrincipal);
        System.out.println("角色信息:"+user);
        //授权角色信息
        if(!CollectionUtils.isEmpty(user.getRoles())){
            SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
            //遍历角色
           user.getRoles().forEach(role -> {
                 simpleAuthorizationInfo.addRole(role.getNameRole());
                 //权限信息
//                List<Perms> permsByRoleId = userService.findPermsByRoleId(role.getId());
//                for (Perms perms : permsByRoleId) {
//                    simpleAuthorizationInfo.addStringPermission(perms.getName());
//                }

            });
            return simpleAuthorizationInfo;
        }
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       String principal = (String) authenticationToken.getPrincipal();
       //在工厂中获取service对象
        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
        System.out.println(userService);
        User user= userService.findByUserName(principal);
        System.out.println(user);

        if(!ObjectUtils.isEmpty(user)){
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),
                    ByteSource.Util.bytes(user.getSalt()),this.getName());
            return simpleAuthenticationInfo;
        }

       /* SimpleAuthenticationInfo simpleAuthorizationInfo=new SimpleAuthenticationInfo("zhuxiangyu","123",this.getName());*/
        /*if("zhuxiangyu".equals(principal)){
            return simpleAuthorizationInfo;
        }*/
        return null;
    }
}
